var RepairCountBoard = function () {
  var template = /* html */ `
    <div class="repair-count-board" v-loading.fullscreen.lock="loading">
      <template v-if="repairData!==null">
        <div class="count-board-title">
          <span>{{moment(oldSearch.year).get('year')}}年月维修量统计图</span>
          <div class="count-board-search">
            <el-date-picker v-model="search.year" type="year" size="small" style="width:100px" :editable="false" :clearable="false"
              @change="searchData">
            </el-date-picker>
          </div>
        </div>
        <div class="count-board-content" :key="'year_'+oldSearch.year">
          <repair-count-chart v-if="repairData.nameList.length>0" :repair-data="repairData" :search="oldSearch"></repair-count-chart>
          <div v-else class="no-data" style="color:#ffffff;font-size:20px">
            <div>暂无月维修量统计数据</div>
          </div>
        </div>
      </template>
    </div>
  `;

  return {
    mixins: [boardRunningMixin],

    data: function () {
      var search = {
        year: moment().startOf('year').valueOf()
      };
      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        repairData: null,
        moment: moment
      };
    },

    created: function () {
      var self = this;
      this.setRepairList(true, true).then(function () {
        self.boardRunning();
      });
    },

    methods: {
      setRepairList: function (isSearch, hasLoading) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var search = this.search;
        var year = search.year;
        var yearTime = moment(year).get('year');

        if (hasLoading) {
          this.loading = true;
        }

        var self = this;
        return ajaxRequest('workOrder/getWorkOrderApplyDataEveryMonth', {
          yearTime: yearTime
        }).then(function (repairList) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          if (repairList === null) {
            repairList = [];
          }

          var repairData = new RepairCountInfo(repairList, year);
          self.repairData = repairData;

          if (hasLoading) {
            self.loading = false;
          }
        }).catch(function () {
          if (hasLoading) {
            self.loading = false;
          }
          return [];
        });
      },

      searchData: function () {
        var self = this;
        this.setRepairList(true, true).then(function () {
          self.boardRunning();
        });
      },

      boardRunning: function () {
        this.clearTimeoutList();

        this.resetRepairListRunning();
      },

      resetRepairListRunning: function () {
        var self = this;
        var timeout = setTimeout(function () {
          var year = self.search.year;
          self.timeoutList[0] = null;
          if (moment(year).isSame(moment(), 'year')) {
            self.setRepairList().then(function () {
              self.resetRepairListRunning();
            });
            return;
          }
          self.resetRepairListRunning();
        }, 3600 * 1000);
        this.timeoutList[0] = timeout;
      }
    },

    components: {
      RepairCountChart: RepairCountChart
    },

    template: template
  };
}();
